﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PeiYue.Api.Controllers.BASE;
using PeiYue.Common.Helper;
using PeiYue.Common.HttpContextUser;
using PeiYue.IServices.Sys;
using PeiYue.Model;
using PeiYue.Model.Dto.User;
using PeiYue.Model.Models.User;

namespace PeiYue.Api.Controllers.User
{
    /// <summary>
    /// 部门
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DepartmentController : BaseController<Department>
    {
        readonly IMapper _mapper;
        readonly IDepartmentServices _departmentServices;
        IUser _user;
        public DepartmentController(IDepartmentServices departmentServices, IUser user, IMapper mapper) : base(departmentServices, user)
        {
            _departmentServices = departmentServices;
            _user = user;
            this._mapper = mapper;

        }
        /// <summary>
        /// 查询分页列表
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<DepartmentDto>>> GetPage([FromQuery] SearchDepartmentDto querymodel)
        {
            Expression<Func<Department, bool>> ex = t => 1 == 1;

            if (!string.IsNullOrEmpty(querymodel.names))
                ex = ex.And(t => t.names.Contains(querymodel.names));
            if (querymodel.status == null|| querymodel.status == 0)
                ex = ex.And(t => t.status != 3);
            else
                ex = ex.And(t => t.status == querymodel.status);

            PageModel<DepartmentDto> data = new PageModel<DepartmentDto>() { };

            var list = await _departmentServices.QueryPage(ex, querymodel.pageIndex, querymodel.pageSize, " Id desc ");
            data = new PageModel<DepartmentDto>() { dataCount = list.dataCount, page = list.page, PageSize = list.PageSize, data = _mapper.Map<List<DepartmentDto>>(list.data) };
            return new MessageModel<PageModel<DepartmentDto>>()
            { msg = "获取成功", success = true, response = data };
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Save([FromBody] DepartmentDto model)
        {
            var dao = _mapper.Map<Department>(model);
            dao.update_user = _user.ID;
            dao.create_user = _user.ID;
            dao.update_time = DateTime.Now;
            var data = new MessageModel<string>();
            if (model.id > 0)
            {

                data.success = await _departmentServices.Update(dao, null, new List<string> { "status", "create_time", "create_user" });
            }
            else
            {
                dao.create_user = _user.ID;
                dao.create_time = DateTime.Now;
                dao.id = await _departmentServices.Add(dao);
                data.success = dao.id > 0;
            }
            if (data.success)
            {
                data.msg = "保存成功";
                data.response = dao?.id.ObjToString();
            }
            return data;
        }
    }
}
